<?php defined('SYSPATH') or die('No direct script access.');

class Input
{
	/**
	 * Fetch an item from the $_POST array.
	 *
	 * @param   string   key to find
	 * @param   mixed    default value
	 * @param   boolean  XSS clean the value
	 * @return  mixed
	 */
	public static function post ($key = '', $default = NULL, $xss_clean = false )
	{
		return self::search_array($_POST, $key, $default, $xss_clean);
	}

	/**
	 * Fetch an item from the $_GET array.
	 *
	 * @param   string   key to find
	 * @param   mixed    default value
	 * @param   boolean  XSS clean the value
	 * @return  mixed
	 */
	public static function get( $key = '', $default = NULL, $xss_clean = false)
	{
		return self::search_array($_GET, $key, $default, $xss_clean);
	}

	/**
	 * Fetch an item from the $_FILE array.
	 *
	 * @param   string   key to find
	 * @param   mixed    default value
	 * @param   boolean  XSS clean the value
	 * @return  mixed
	 */
	public static function file( $key = '', $default = NULL, $xss_clean = false)
	{
		return self::search_array($_FILES, $key, $default, $xss_clean);
	}

	/**
	 * Fetch an item from the $_SERVER array.
	 *
	 * @param   string   key to find
	 * @param   mixed    default value
	 * @param   boolean  XSS clean the value
	 * @return  mixed
	 */
	public static function server( $key = '', $default = NULL, $xss_clean = false)
	{
		return self::search_array($_SERVER, $key, $default, $xss_clean);
	}

	/**
	 * Fetch an item from a global array.
	 *
	 * @param   array    array to search
	 * @param   string   key to find
	 * @param   mixed    default value
	 * @param   boolean  XSS clean the value
	 * @return  mixed
	 */
	public static function search_array( $array = array(), $key = '', $default = NULL, $xss_clean = false )
	{
		if(substr_count($key, '.') > 0)
		{
			$value = arr::path($array, $key, $default);
		}
		else
		{
			$value = arr::get($array, $key, $default);
		}

		if( $xss_clean AND !is_array($value))
		{
			$value = Security::xss_clean($value);
		}

		return $value;
	}
}